//
//  FQDLabVoteResultCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/18/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDLabVoteResultCell.h"
#import "FQDPieChart.h"
#import "FQDRestModel.h"

#import "Masonry.h"

@interface FQDLabVoteResultCell ()

@property (nonatomic, strong) FQDPieChart *pieChart;
@property (nonatomic, strong) UILabel *percentLabel;
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation FQDLabVoteResultCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    _pieChart = [[FQDPieChart alloc] init];
    _pieChart.fillColor = RGB(235, 235, 235);
    [self.contentView addSubview:_pieChart];
    
    _percentLabel = [[UILabel alloc] init];
    _percentLabel.font = FQDBoldFontWithSize(15.0f);
    _percentLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_percentLabel];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = FQDRegularFontWithSize(15.0f);
    _contentLabel.textColor = RGB(96, 96, 96);
    _contentLabel.numberOfLines = 2;
    _contentLabel.preferredMaxLayoutWidth = 210.0f;
    [self.contentView addSubview:_contentLabel];
}

- (void)prepareConstraints
{
    WeakSelf;
    
    [_pieChart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.contentView).offset(20);
        make.size.equalTo(@50);
    }];
    
    [_percentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.pieChart.mas_right).offset(20);
    }];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.percentLabel.mas_bottom).offset(6);
        make.left.equalTo(weakSelf.percentLabel);
    }];
}

- (void)setAttitude:(FQDLabVoteAttitude *)attitude
{
    _attitude = attitude;
    
    self.contentLabel.text = attitude.content;
    self.percentLabel.text = [NSString stringWithFormat:@"%@%%", @(attitude.percent)];
    
    UIColor *fillColor = (self.isMyChoice) ? RGB(255, 233, 165) : RGB(235, 235, 235);
    UIColor *sliceColor = (self.isMyChoice) ? RGB(255, 199, 31) : RGB(204, 204, 204);
    FQDPieChartSlice *slice = [FQDPieChartSlice sliceWithPercent:(attitude.percent / 100.0f) color:sliceColor];
    self.pieChart.fillColor = fillColor;
    self.pieChart.slices = @[slice];
}

+ (NSString *)reuseId
{
    return @"FQDLabVoteResultCell";
}

@end
